package com.example.demo.controller.factory;

/**
 * @author 徐塬峰
 * @version 1.0
 * @date 2021-04-30 5:25 下午
 * @desc
 */
public class FruitFactory {
    public static Fruit getFruit(String type) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        /*这里通过反射的方式获取到水果子类的字节码，即类对象，通过类对象的newInstance()方法创建水果子类
        实例*/
        Class fruit = Class.forName(FruitFactory.class.getPackage().getName()+"."+type);
        return (Fruit) fruit.newInstance();
    }
}
